package me.aip.mov.controller;

import me.aip.mov.annotation.GlobalResult;
import me.aip.mov.annotation.PassToken;
import me.aip.mov.entity.AccessUser;
import me.aip.mov.entity.UserInfo;
import me.aip.mov.response.ResponseResult;
import me.aip.mov.service.IAuthService;
import me.aip.mov.service.IUserInfoService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Map;


@RestController
@RequestMapping("/auth")
@GlobalResult
public class AuthController {

    @Autowired
    IAuthService authService;

    @Autowired
    IUserInfoService userInfoService;
    @GetMapping("/getLoginRsaKey")
    @PassToken
    public ResponseResult getLoginRsaKey() throws Exception {
        Map<String, String> rsaKeyMap = authService.getRsakey();
        return ResponseResult.success(rsaKeyMap);
    }

    @PostMapping("/loginByPhone")
    public ResponseResult loginByPhone(@RequestBody AccessUser accessUser) {
        if (StringUtils.isBlank(accessUser.getRsakey()) || StringUtils.isBlank(accessUser.getPhone())
                || StringUtils.isBlank(accessUser.getPasswd())) {
            return ResponseResult.failure("参数缺失！");
        }
        UserInfo user = authService.loginByPhone(accessUser);

        return ResponseResult.success();
    }

    @PostMapping({"/login"})
    @PassToken
    public ResponseResult login(@RequestBody AccessUser accessUser) {
        if (StringUtils.isBlank(accessUser.getUsername())) {
            return ResponseResult.failure("账号不能为空！");
        }
        if (StringUtils.isBlank(accessUser.getPasswd())) {
            return ResponseResult.failure("密码不能为空！");

        }

        String passwd = authService.getDecodePassword(accessUser.getPasswd(), accessUser.getRsakey());
        ResponseResult login = null;
        try {
             login = authService.login(accessUser.getUsername(), passwd);

        }catch (Exception e){
            return ResponseResult.failure(e.getMessage());
        }
        return  login;
    }

    @PostMapping({"/loginByWx"})
    public ResponseResult loginByWx(String wxCode, String appId) {
        return authService.loginByWx(wxCode, appId);
    }

    @PostMapping({"/loginByWxApplet"})
    public ResponseResult loginByWxApplet(String wxCode, String appId, String encryptedData, String iv) {
        return authService.loginByWxApplet(wxCode, appId, encryptedData, iv);
    }

    @PostMapping({"/loginByQQ"})
    public ResponseResult loginByQQ(String openId, String unionid, String nickName, String city, String province, String headimgurl, String sex, String rsakey) {
        openId = authService.getDecodePassword(openId, rsakey);
        unionid = authService.getDecodePassword(unionid, rsakey);
        if (StringUtils.isBlank(openId)) {
            return ResponseResult.failure("qq用户授权错误！");
        }
        return authService.loginByQQ(openId, unionid, nickName, city, province, headimgurl, sex);
    }

    @PostMapping({"/loginByApple"})
    public ResponseResult loginByApple(String openId, String unionid, String nickName, String rsakey) {
        openId = authService.getDecodePassword(openId, rsakey);
        unionid = authService.getDecodePassword(unionid, rsakey);
        if (StringUtils.isBlank(openId)) {
            return ResponseResult.failure("Apple授权错误！");
        }
        return authService.loginByApple(openId, unionid, nickName);
    }

    @PostMapping({"/getOpenId"})
    public ResponseResult getOpenId(String wxCode, String appId) {
        if (StringUtils.isBlank(wxCode)) {
            return ResponseResult.failure("wxCode不能为空！");
        }
        if (StringUtils.isBlank(appId)) {
            return ResponseResult.failure("appId不能为空！");
        }
        return authService.getOpenInfo(wxCode, appId);
    }

    @PostMapping({"/getOpenIdByJs"})
    public ResponseResult getOpenIdJS(String wxCode, String appId) {
        if (StringUtils.isBlank(wxCode)) {
            return ResponseResult.failure("wxCode不能为空！");
        }
        if (StringUtils.isBlank(appId)) {
            return ResponseResult.failure("appId不能为空！");
        }
        return authService.getOpenInfoJS(wxCode, appId);
    }

    @PostMapping({"/regUserByWx"})
    public ResponseResult regUserByWx(String tel, String wxCode, String smsCode, String source, HttpServletResponse response) {
        if (StringUtils.isBlank(tel)) {
            return ResponseResult.failure("手机号不能为空！");
        }
        if (StringUtils.isBlank(wxCode)) {
            return ResponseResult.failure("wxCode不能为空！");
        }
        if (!authService.validateSmsCode(tel, smsCode)) {
            return ResponseResult.failure("短信验证码校验失败！");
        }
        return authService.creatUserByWx(tel, wxCode, source);
    }

    @PostMapping({"/loginBySms"})
    public ResponseResult loginBySms(String tel, String smsCode, String source, HttpServletResponse response) {
        if (StringUtils.isBlank(tel)) {
            return ResponseResult.failure("手机号不能为空！");
        }
        if (!authService.validateSmsCode(tel, smsCode)) {
            return ResponseResult.failure("短信验证码校验失败！");
        }
        return authService.loginBySms(tel);
    }


    @GetMapping({"/captcha"})
    public void getCaptcha(HttpServletResponse response) throws IOException {

    }

    @PostMapping({"/outLogin"})
    public ResponseResult outLogin(HttpServletRequest request) {
        String token = request.getHeader("token");
        authService.outLogin(token);
        return ResponseResult.success();
    }


    @PostMapping({"/regUserByName"})
    @PassToken
    public ResponseResult regUserByName(@RequestBody AccessUser accessUser) {
        if (StringUtils.isBlank(accessUser.getUsername())) {
            return ResponseResult.failure("账号不能为空！");
        }
        if (StringUtils.isBlank(accessUser.getPasswd())) {
            return ResponseResult.failure("密码不能为空！");
        }
        UserInfo userByUsername = userInfoService.getUserByUsername(accessUser.getUsername());
        if (userByUsername != null){
            return ResponseResult.failure("账号已存在！");
        }
        String passwd = authService.getDecodePassword(accessUser.getPasswd(), accessUser.getRsakey());
        return authService.creatUserByUserName(accessUser.getUsername(), passwd,null);
    }

    @PostMapping({"/regUserByPhone"})
    @PassToken
    public ResponseResult regUserByPwd(String tel, String passwd, String smsCode, String source, String rsakey) {
        if (StringUtils.isBlank(tel)) {
            return ResponseResult.failure("手机号不能为空！");
        }
        if (StringUtils.isBlank(passwd)) {
            return ResponseResult.failure("密码不能为空！");
        }
        if (!authService.validateSmsCode(tel, smsCode)) {
            return ResponseResult.failure("短信验证码校验失败！");
        }
        passwd = authService.getDecodePassword(passwd, rsakey);
        return authService.creatUserByPhone(tel, passwd, source);
    }

    @PostMapping({"/forgetPassword"})
    public ResponseResult forgetPassword(String tel, String passwd, String smsCode, String rsakey, HttpServletResponse response) {
        if (!authService.validateSmsCode(tel, smsCode)) {
            return ResponseResult.failure("短信验证码校验失败！");
        }
        if (StringUtils.isBlank(tel)) {
            return ResponseResult.failure("手机号不能为空！");
        }
        if (StringUtils.isBlank(passwd)) {
            return ResponseResult.failure("密码不能为空！");
        }
        passwd = authService.getDecodePassword(passwd, rsakey);
        return authService.forgetPassword(tel, passwd);
    }

    @PostMapping({"/changePassword"})
    public ResponseResult changePassword(String userId, String passwd, String smsCode, String rsakey) {
        if (StringUtils.isBlank(userId)) {
            return ResponseResult.failure("用户身份校验失败！");
        }
        if (StringUtils.isBlank(passwd)) {
            return ResponseResult.failure("密码不能为空！");
        }
        passwd = authService.getDecodePassword(passwd, rsakey);
        return authService.changePassword(userId, passwd, smsCode);
    }

    @PostMapping({"/sendSms"})
    private ResponseResult sendSms(String tel, String captchaCode, HttpServletResponse response) {
        if (authService.validateCaptchaCode(captchaCode)) {
            return ResponseResult.failure("验证码错误或已经失效！");

        }
        if (StringUtils.isBlank(tel)) {
            return ResponseResult.failure("手机号不能为空！");
        }
        return authService.sendSms(tel);
    }


}
